import config

names = config.database.split(',')
print('=============================================创建用户=================================================')
print(f"CREATE USER '{config.config.get('lfll_prod')}'@'%' IDENTIFIED BY '{config.config.get('mysql_password')}';")
print('=============================================创建数据库===============================================')
for name in names:
    database = f"{config.config.get('lfll_prod')}_{name}"
    print(f"CREATE DATABASE {database} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;")
print(f"CREATE DATABASE {config.nacos_db} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;")
print(f"CREATE DATABASE {config.xxljob_db} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;")
print('=============================================数据库授权===============================================')
for name in names:
    print(f"GRANT ALL PRIVILEGES ON {database}.* TO '{config.config.get('lfll_prod')}'@'%';")
print(f"GRANT ALL PRIVILEGES ON {config.nacos_db}.* TO '{config.config.get('lfll_prod')}'@'%';")
print(f"GRANT ALL PRIVILEGES ON {config.xxljob_db}.* TO '{config.config.get('lfll_prod')}'@'%';")
for name in names:
    if 'old_tc' in name:
        print('=====================================跑liquibase后执行sql=============================================')
        # print(database)
        print("CREATE TABLE `" + database + "`.`job_mapping` ( `id` int NOT NULL AUTO_INCREMENT, `r_datasource` varchar(255) DEFAULT NULL, `r_dbname` varchar(255) DEFAULT NULL, `r_tablename` varchar(255) DEFAULT NULL, `r_column` varchar(255) DEFAULT NULL, `w_datasource` varchar(255) DEFAULT NULL, `w_dbname` varchar(255) DEFAULT NULL, `w_tablename` varchar(255) DEFAULT NULL, `w_column` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE=InnoDB ROW_FORMAT=DYNAMIC;")
        print('================================授权mysql.help_topic用户查询权限=======================================')
        print("grant select on mysql.help_topic to " + "'" + config.config.get('lfll_prod') + "'@'%';")
        print('========================================old_tc库初始化================================================')
        print("INSERT INTO `" + database + "`.`sys_user` (`user_id`, `dept_id`, `user_name`, `nick_name`, `user_type`, `email`, `phonenumber`, `sex`, `avatar`, `password`, `status`, `del_flag`, `login_ip`, `login_date`, `create_by`, `create_time`, `update_by`, `update_time`, `remark`, `qwx_account`, `corpant_id`, `qr_code`, `corp_id`, `corp_name`, `auth_type`, `valid_start_date`, `valid_end_date`, `resign_status`) VALUES (1, 1, 'admin', 'peter', '00', 'peter.yu@diact.com', '15618917066', '0', 'https://ipsos-dia.obs.cn-east-3.myhuaweicloud.com/2021/04/29/32296ea8-6975-4cc7-af0e-6c2299f59c30.jpeg', '$2a$10$1RDfF166YvwFNuwmzFelDe4N3EZz1zJeVXA8ReMt7kaQhNXEJ7tJi', '0', '0', '127.0.0.1', NULL, 'admin', '2018-03-16 11:33:00', 'ry', '2022-07-01 17:08:09', '管理员', 'peter', 4, '', NULL, NULL, NULL, NULL, NULL, 0);")
        print("INSERT INTO `" + database + "`.`sys_role` (`role_id`, `role_name`, `role_key`, `role_sort`, `data_scope`, `menu_check_strictly`, `dept_check_strictly`, `status`, `del_flag`, `create_by`, `create_time`, `update_by`, `update_time`, `remark`, `user_id`, `dept_id`) VALUES (1, '超级管理员', 'admin', 1, '1', 1, 1, '0', '0', 'admin', '2018-03-16 11:33:00', 'ry', '2018-03-16 11:33:00', '超级管理员', 1, 103);")
        print("INSERT INTO `" + database + "`.`sys_user_role` (`user_id`, `role_id`) VALUES (1, 1);")
        print("INSERT INTO `" + database + "`.`sys_dept` (`dept_id`, `parent_id`, `ancestors`, `dept_name`, `order_num`, `leader`, `phone`, `email`, `status`, `del_flag`, `create_by`, `create_time`, `update_by`, `update_time`, `user_id`, `corpant_id`, `corp_id`, `corp_name`, `qw_dept_id`, `qw_dept_name`) VALUES (1, 0, '0', 'DIA', 100000000, NULL, NULL, NULL, '0', '0', 'admin', '2021-03-30 18:23:28', 'scrio.zou', '2023-01-03 15:22:13', 1, 4, NULL, NULL, NULL, NULL);")
        print("ALTER TABLE `" + database + "`.`biz_custom_object_attribute_setting` MODIFY COLUMN `field_name` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' COMMENT '字段名称' AFTER `field_code`;")
        print("ALTER TABLE `" + database + "`.`biz_custom_object_attribute_setting` MODIFY COLUMN `field_code` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '字段key' AFTER `create_type`;")
    if 'opms' in name:
        print('=========================================opms库更改oss配置============================================')
        print("DROP TABLE IF EXISTS `" + database + "`.`tools_setting`;")
        print("CREATE TABLE `" + database + "`.`tools_setting`  (`key` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '0' COMMENT '设置项标示', `describe` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL DEFAULT '0' COMMENT '设置项描述', `values` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '设置内容(json格式)', `create_time` datetime NOT NULL COMMENT '创建时间', `update_time` datetime NULL DEFAULT NULL COMMENT '更新时间' ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '设置表' ROW_FORMAT = DYNAMIC;")
        print("INSERT INTO `" + database + "`.`tools_setting` (`key`, `describe`, `values`, `create_time`, `update_time`) VALUES ('storage', '上传设置', '{\"default\": \"aliyun\",\"engine\": {\"local\": {\"bucket\": \"" + config.bucketname + "\",\"access_key_id\": \"LTAI5tRgHp7pUVaaLMZLwUJi\",\"access_key_secret\":\"4fYzRTz5iKGVD5zYcH5d0BVbiuYUXf\",\"domain\": \"" + config.damin +"\",\"end_point\": \"oss-cn-shanghai.aliyuncs.com\"}}}', '2024-04-28 20:15:00', NULL); SET FOREIGN_KEY_CHECKS = 1;")
        print('======================================opms库添加tentant表配置==========================================')
        print("INSERT INTO `lfll_dev_opms`.`tenant` (`id`, `tenant_name`, `tenant_balance`, `tenant_date`, `tenant_contacts`, `tenant_phone`, `sys_account`, `sys_password`, `sys_project`, `db_url`, `db_username`, `db_password`, `db_driver`, `del_flag`, `tenant_sld`, `tenant_name_ch`, `tenant_name_en`, `star_level`, `tenant_desc`, `ext_reminder`, `tenant_email`, `sys_logo`, `create_by`, `create_time`, `update_by`, `update_time`, `tenant_status`, `software_version`, `sys_title`, `home_page`, `mail_count`, `message_count`, `applet_count`, `wechat_count`, `db_name`, `tenant_domain`, `rec_count`, `is_free`, `sys_title_icon`, `enterprise_code`, `domain`, `tenant_code`, `privatization_deployment`) VALUES (1, 'DIA-运营管理平台', 0.00, '2025-06-26 00:00:00', 'peter', '15618917066', NULL, '$2a$10$1RDfF166YvwFNuwmzFelDe4N3EZz1zJeVXA8ReMt7kaQhNXEJ7tJi', NULL, '10.137.142.233:30006', 'siit', 'siit2021', NULL, '0', 'opms', 'opms', 'opms', NULL, '', '0', 'peter.yu@diact.com', 'https://dia-pre.oss-cn-beijing.aliyuncs.com/opms/2022/09/23/45c3bc99-a41c-4fdf-a7e7-06ea11b845a1/logo_dia_opms_白色.png', 'admin', '2021-06-16 23:38:02', 'admin', '2022-09-23 11:49:27', 'use', 'standard', '', '', 100, 100, 100, 100, 'opms', 'https://" + config.config.get('opms_com') + "'" + ", 100000, 2, NULL, 'pre-opms', NULL, NULL, 1);")
        print("INSERT INTO `lfll_dev_opms`.`tenant` (`id`, `tenant_name`, `tenant_balance`, `tenant_date`, `tenant_contacts`, `tenant_phone`, `sys_account`, `sys_password`, `sys_project`, `db_url`, `db_username`, `db_password`, `db_driver`, `del_flag`, `tenant_sld`, `tenant_name_ch`, `tenant_name_en`, `star_level`, `tenant_desc`, `ext_reminder`, `tenant_email`, `sys_logo`, `create_by`, `create_time`, `update_by`, `update_time`, `tenant_status`, `software_version`, `sys_title`, `home_page`, `mail_count`, `message_count`, `applet_count`, `wechat_count`, `db_name`, `tenant_domain`, `rec_count`, `is_free`, `sys_title_icon`, `enterprise_code`, `domain`, `tenant_code`, `privatization_deployment`) VALUES (2, '本地" + config.config.get('cem_name') + "', 0.00, '2025-06-26 00:00:00', 'peter', '15618917066', NULL, 'false', 'SCRM', '10.137.142.233:30006', 'siit', 'siit2021', NULL, '0', '" +  config.config.get('cem_name') +"', '" + config.config.get('cem_name') + "', '" + config.config.get('cem_name') + "', NULL, '', '0', 'peter.yu@diact.com', 'https://dia-pre.oss-cn-beijing.aliyuncs.com/opms/2022/10/20/af5d3cd3-349f-4009-ad62-9a6eeecc947d/WechatIMG10.png', 'admin', '2021-06-16 23:38:02', 'admin', '2023-06-16 19:22:36', 'use', 'VIP', '', 'index', 100, 100, 100, 100, 'pre-test', 'https://" + config.config.get('cem_com') + "', 100000000, 1, '本地" + config.config.get('cem_name') + "', NULL, NULL, NULL, 1);  ")

print("FLUSH PRIVILEGES;")
print('========================================clickhouse创建数据库===========================================')
print("CREATE DATABASE " + config.config.get('lfll_prod') + ";")